<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-select multiple v-model="queryParams.tagidList" filterable clearable placeholder="用户标签">
          <el-option v-for="item in wxUserTags" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="城市" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入城市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扫码场景值" prop="qrSceneStr" label-width="120px">
        <el-input
          v-model="queryParams.qrSceneStr"
          placeholder="请输入扫码场景值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          :disabled="ids.length <= 0"
          @click="$refs.userTagging.init('tagging')"
          v-hasPermi="['wx:user:save']"
        >绑定标签</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          :disabled="ids.length <= 0"
          @click="$refs.userTagging.init('untagging')"
          v-hasPermi="['wx:user:save']"
        >解绑标签</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wx:user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wx:user:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-price-tag"
          size="mini"
          @click="$refs.wxUserTagsEditor.show()"
        >标签管理</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="syncWxUsers()"
        >同步粉丝</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="userList" @selection-change="handleSelectionChange" :default-sort="defaultSort" @sort-change="handleSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="openid" align="center" prop="openid" v-if="true"/>
      <el-table-column label="昵称" align="center" prop="nickname" />
      <el-table-column label="性别" align="center" prop="sex" />
      <el-table-column label="城市" align="center" prop="city" />
      <el-table-column label="头像" align="center" prop="headimgurl" />
      <el-table-column label="订阅时间" align="center" prop="subscribeTime" sortable="custom" :sort-orders="['descending', 'ascending']" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.subscribeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否关注" align="center" prop="subscribe" />
      <el-table-column label="标签" align="center" prop="tagidList">
        <template slot-scope="scope">
          <span v-for="tagid in scope.row.tagidList" :key="tagid">{{getTagName(tagid)}} </span>
        </template>
      </el-table-column>
      <el-table-column label="关注场景" align="center" prop="subscribeScene" />
      <el-table-column label="场景值" align="center" prop="qrSceneStr" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wx:user:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <wx-user-tags-manager ref="wxUserTagsEditor" :visible="showWxUserTagsEditor" @close="showWxUserTagsEditor=false"></wx-user-tags-manager>
    <user-tagging ref="userTagging" :wxUsers="dataListSelections"></user-tagging>
  </div>
</template>

<script>
import { listUser, delUser, syncWxUsers } from "@/api/wx/user";
import WxUserTagsManager from '@/components/wx-user-tags-manager'
import UserTagging from './user-tagging'
import { mapState } from 'vuex'
export default {
  name: "User",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 默认排序
      defaultSort: {prop: 'subscribeTime', order: 'descending'},
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        appid: undefined,
        phone: undefined,
        nickname: undefined,
        sex: undefined,
        city: undefined,
        province: undefined,
        headimgurl: undefined,
        subscribeTime: undefined,
        subscribe: undefined,
        unionid: undefined,
        tagidList: undefined,
        subscribeScene: undefined,
        qrSceneStr: undefined
      },
      showWxUserTagsEditor:false,
      dataListSelections: [],
    };
  },
  components: {
    WxUserTagsManager,UserTagging
  },
  created() {
    this.queryParams.orderByColumn = this.defaultSort.prop;
    this.queryParams.isAsc = this.defaultSort.order;
    this.getList();
  },
  computed: mapState({
    wxUserTags:state=>state.wxUserTags.tags
  }),
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.openid)
      this.dataListSelections = selection
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 排序触发事件 */
    handleSortChange(column) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const openids = row.openid || this.ids;
      this.$modal.confirm('是否确认删除用户编号为"' + openids + '"的数据项？').then(() => {
        this.loading = true;
        return delUser(openids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wx/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    },
    syncWxUsers(){
      syncWxUsers().then(response => {
        this.$message({
          message: '同步任务已建立，请稍候刷新查看列表',
          type: 'success',
          duration: 1500
        })
      }).finally(() => {

      });
    },
    getTagName(tagid){
      let tag = this.wxUserTags.find(item=>item.id==tagid)
      return tag?tag.name : "?"
    }
  }
};
</script>
